define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'order/index' + location.search,
                    add_url: 'order/add',
                    // edit_url: 'order/edit',
                    del_url: 'order/del',
                    multi_url: 'order/multi',
                    table: 'order',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        // {field: 'id', title: __('Id')},
                        {field: 'shop_name', title: __('Shop_name'), operate: 'like'},
                        {field: 'out_trade_no', title: __('Out_trade_no'), operate: 'like'},
                        {field: 'channel_type', title: __('Channel_type'), searchList: {1:'微信小程序', 2:'支付宝小程序'}, formatter: Table.api.formatter.label},
                        {field: 'send_type', title: __('Send_type'), searchList: {1:'到店自取', 2:'送车上门'}, formatter: Table.api.formatter.label},
                        {field: 'deposit_type', title: __('Deposit_type'), searchList: {1:'门店预授权', 2:'门店信用免押'}, formatter: Table.api.formatter.label},
                        {field: 'username', title: __('User_id'), operate: 'like', formatter: function (value, row, index) {
                                return row.user.username + '</br>' + row.user_phone;
                            }, align:"left"},
                        {field: 'car_image', title: __('Car_image'), events: Table.api.events.image, formatter: Table.api.formatter.image,operate: false},
                        {field: 'car_name', title: __('Car_name'), operate: 'like', formatter: function (value, row, index) {
                                return row.car_name + '</br><span style="color: red">' + row.car_number + '</span>';
                            }, align:"left"},
                        {field: 'pick_address', title: __('Pick_return_address'), operate: 'like', formatter: function (value, row, index) {
                                return '取：' + row.pick_address + '</br>还：' + row.return_address + '</span>';
                            }, align:"left"},
                        {field: 'time_slot', title: __('Time_slot'), operate:false, formatter: function (value, row, index) {
                                return '取：' + row.start_time + '</br>还：' + row.end_time + '</span>';
                            }, align:"left"},
                        {field: 'days', title: __('Days'), operate:false},
                        {field: 'rental_money', title: __('Rental_money'), operate:'BETWEEN'},
                        {field: 'handle_fee', title: __('Handle_fee'), operate:false},
                        {field: 'insurance', title: __('Insurance'), operate:false},
                        {field: 'distance', title: __('Distance'), operate:false},
                        {field: 'distance_price', title: __('Distance_price'), operate:false},
                        {field: 'total_distance_price', title: __('Total_distance_price'), operate:false},

                        {field: 'night_pick_fee', title: __('Night_pick_fee'), operate:false},
                        {field: 'night_return_fee', title: __('Night_return_fee'), operate:false},
                        {field: 'coupon_money', title: __('Coupon_money'), operate:false},
                        {field: 'face_money', title: __('Face_money'), operate:false},
                        {field: 'all_money', title: __('All_money'), operate:false},
                        // {field: 'return_address', title: __('Return_address'), operate: 'like'},
                        {field: 'status', title: __('Status'), searchList: {1:'已预约', 2:'用车中', 3:'已完成', 4:'已取消'}, formatter: Table.api.formatter.status},
                        {field: 'change_status', title: __('Change_Status'), searchList: Config.changeStatusList, formatter: Table.api.formatter.status},
                        {field: 'continue_status', title: __('Continue_Status'), searchList: Config.continueStatusList, formatter: Table.api.formatter.status},
                        {field: 'face_status', title: __('Face_Status'), searchList: Config.faceStatusList, formatter: Table.api.formatter.status},
                        // {field: 'deposit_status', title: __('Deposit_status'), searchList: {0:'无押金', 1:'待退还', 2:'已原路退还'}, formatter: Table.api.formatter.status},
                        {field: 'all_change_refund_money', title: __('All_change_refund_money'), operate: 'between'},
                        {field: 'refund_status', title: __('Refund_status'), searchList: Config.refundStatusList, formatter: Table.api.formatter.status},
                        {field: 'order_refund_money', title: __('Order_refund_money'), operate: 'between'},
                        {field: 'order_refund_status', title: __('Order_refund_status'), searchList: Config.orderRefundStatusList, formatter: Table.api.formatter.status},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {
                            field: 'buttons',
                            width: "120px",
                            title: __('其他订单'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'change',
                                    text: function (row) {
                                        return '换车订单(' + row.change_num + ')';
                                    },
                                    title: __('换车订单'),
                                    icon: 'fa fa-list',
                                    classname: 'btn btn-xs btn-info btn-dialog',
                                    url: function (row) {
                                        return 'order_change/index?order_id=' + row.id;
                                    }
                                },
                                {
                                    name: 'continue',
                                    text:  function (row) {
                                        return '续租订单(' + row.continue_num + ')';
                                    },
                                    title: __('续租订单'),
                                    icon: 'fa fa-list',
                                    classname: 'btn btn-xs btn-success btn-dialog',
                                    url: function (row) {
                                        return 'order_continue/index?order_id=' + row.id;
                                    }
                                },
                                {
                                    name: 'face',
                                    text: function (row) {
                                        return '当面付订单(' + row.face_num + ')';
                                    },
                                    title: __('当面付订单'),
                                    icon: 'fa fa-list',
                                    classname: 'btn btn-xs btn-warning btn-dialog',
                                    url: function (row) {
                                        return 'order_face/index?order_id=' + row.id;
                                    }
                                },
                            ],
                            formatter: Table.api.formatter.buttons
                        },
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate, buttons: [
                                {
                                    name:'giveback',
                                    text:'订单退款',
                                    title:'订单退款',
                                    classname:'btn btn-xs btn-success btn-dialog',
                                    url: 'order/giveback',
                                    visible: function (row) {
                                        if(row.status == 3 && row.change_status != 2 && (row.continue_status != 2 && row.continue_status != 3) && row.face_status != 2 && (row.order_refund_status != 2 && row.order_refund_status != 3)) return true;
                                        return false;
                                    },
                                    success:function (res){
                                        // table.bootstrapTable('refresh');
                                        parent.Toastr.success("退款成功！");//成功后提示
                                        $("a.btn-refresh").trigger("click");
                                        layer.closeAll(index);
                                        return false;
                                    },
                                    error: function (data, ret) {
                                        layer.closeAll();
                                        return false;
                                    },
                                },
                                {
                                    name:'refund_adopt',
                                    text:'订单退款通过',
                                    title:'订单退款通过',
                                    classname:'btn btn-xs btn-success btn-ajax',
                                    url: 'order/refund?type=1',
                                    confirm: '确认通过？',
                                    visible: function (row) {
                                        if(row.status == 3 && row.order_refund_status == 2) return true;
                                        return false;
                                    },
                                    success:function (res){
                                        table.bootstrapTable('refresh');
                                    },
                                    error: function (data, ret) {
                                        return false;
                                    },
                                },
                                {
                                    name:'refund_refuse',
                                    text:'订单退款拒绝',
                                    title:'订单退款拒绝',
                                    classname:'btn btn-xs btn-danger btn-ajax',
                                    url: 'order/refund?type=2',
                                    confirm: '确认拒绝？',
                                    visible: function (row) {
                                        if(row.status == 3 && row.order_refund_status == 2) return true;
                                        return false;
                                    },
                                    success:function (res){
                                        table.bootstrapTable('refresh');
                                    },
                                    error: function (data, ret) {
                                        return false;
                                    },
                                },
                                {
                                    name:'adopt',
                                    text:'换车退款通过',
                                    title:'换车退款通过',
                                    classname:'btn btn-xs btn-success btn-ajax',
                                    url: 'order/examine?type=1',
                                    confirm: '确认通过？',
                                    visible: function (row) {
                                        if(row.status == 3 && row.refund_status == 2) return true;
                                        return false;
                                    },
                                    success:function (res){
                                        table.bootstrapTable('refresh');
                                    },
                                    error: function (data, ret) {
                                        return false;
                                    },
                                },
                                {
                                    name:'refuse',
                                    text:'换车退款拒绝',
                                    title:'换车退款拒绝',
                                    classname:'btn btn-xs btn-danger btn-ajax',
                                    url: 'order/examine?type=2',
                                    confirm: '确认拒绝？',
                                    visible: function (row) {
                                        if(row.status == 3 && row.refund_status == 2) return true;
                                        return false;
                                    },
                                    success:function (res){
                                        table.bootstrapTable('refresh');
                                    },
                                    error: function (data, ret) {
                                        return false;
                                    },
                                },

                                {
                                    name:'pick',
                                    text:'取车',
                                    title:'取车',
                                    classname:'btn btn-xs btn-success btn-dialog',
                                    url: 'order/pick',
                                    visible: function (row) {
                                        if(row.status == 1 && row.change_status != 2 && (row.continue_status != 2 && row.continue_status != 3) && row.face_status != 2) return true;
                                        return false;
                                    },
                                    success:function (res){
                                        // table.bootstrapTable('refresh');
                                        parent.Toastr.success("取车成功！");//成功后提示
                                        $("a.btn-refresh").trigger("click");
                                        layer.closeAll(index);
                                        return false;
                                    },
                                    error: function (data, ret) {
                                        layer.closeAll();
                                        return false;
                                    },
                                },
                                {
                                    name:'return',
                                    text:'还车',
                                    title:'还车',
                                    classname:'btn btn-xs btn-success btn-dialog',
                                    url: 'order/return',
                                    visible: function (row) {
                                        if(row.status == 2 && row.change_status != 2 && (row.continue_status != 2 && row.continue_status != 3) && row.face_status != 2) return true;
                                        return false;
                                    },
                                    success:function (res){
                                        // table.bootstrapTable('refresh');
                                        parent.Toastr.success("还车成功！");//成功后提示
                                        $("a.btn-refresh").trigger("click");
                                        layer.closeAll(index);
                                        return false;
                                    },
                                    error: function (data, ret) {
                                        layer.closeAll();
                                        return false;
                                    },
                                },
                                {
                                    name:'change',
                                    text:'排车',
                                    title:'排车',
                                    classname:'btn btn-xs btn-warning btn-dialog',
                                    url: function (row) {
                                        return 'order_change/add?order_id='+row.id;
                                    },
                                    visible: function (row) {
                                        if(row.status == 1 && row.change_status != 2 && (row.continue_status != 2 && row.continue_status != 3) && row.face_status != 2) return true;
                                        return false;
                                    },
                                    success:function (res){
                                        // table.bootstrapTable('refresh');
                                        parent.Toastr.success("排车成功！");//成功后提示
                                        $("a.btn-refresh").trigger("click");
                                        layer.closeAll(index);
                                        return false;
                                    },
                                    error: function (data, ret) {
                                        layer.closeAll();
                                        return false;
                                    },
                                },
                                {
                                    name:'continue',
                                    text:'续租',
                                    title:'续租',
                                    classname:'btn btn-xs btn-warning btn-dialog',
                                    url: function (row) {
                                        return 'order_continue/add?order_id='+row.id;
                                    },
                                    visible: function (row) {
                                        if((row.status == 1 || row.status == 2) && row.change_status != 2 && (row.continue_status != 2 && row.continue_status != 3) && row.face_status != 2) return true;
                                        return false;
                                    },
                                    success:function (res){
                                        // table.bootstrapTable('refresh');
                                        parent.Toastr.success("续租成功！");//成功后提示
                                        $("a.btn-refresh").trigger("click");
                                        layer.closeAll(index);
                                        return false;
                                    },
                                    error: function (data, ret) {
                                        layer.closeAll();
                                        return false;
                                    },
                                },
                                {
                                    name:'face',
                                    text:'当面付',
                                    title:'当面付',
                                    classname:'btn btn-xs btn-warning btn-dialog',
                                    url: function (row) {
                                        return 'order_face/add?order_id='+row.id;
                                    },
                                    visible: function (row) {
                                        if((row.status == 1 || row.status == 2 || row.status == 3) && row.change_status != 2 && (row.continue_status != 2 && row.continue_status != 3) && row.face_status != 2) return true;
                                        return false;
                                    },
                                    success:function (res){
                                        // table.bootstrapTable('refresh');
                                        parent.Toastr.success("创建当面付成功！");//成功后提示
                                        $("a.btn-refresh").trigger("click");
                                        layer.closeAll(index);
                                        return false;
                                    },
                                    error: function (data, ret) {
                                        layer.closeAll();
                                        return false;
                                    },
                                },
                            {
                                name:'cancel',
                                text:'取消订单',
                                title:'取消订单',
                                classname:'btn btn-xs btn-success btn-ajax',
                                url: 'order/cancel',
                                confirm: '确认取消？',
                                visible: function (row) {
                                    if(row.status == 1) return true;
                                    return false;
                                },
                                success:function (res){
                                    // table.bootstrapTable('refresh');
                                    parent.Toastr.success("取消成功！");//成功后提示
                                    $("a.btn-refresh").trigger("click");
                                    layer.close(index);
                                    return false;
                                },
                                error: function (data, ret) {
                                    layer.closeAll(index);
                                    return false;
                                },
                            },
                                // {
                                //     name:'deposit',
                                //     text:'退押金',
                                //     title:'退押金',
                                //     classname:'btn btn-xs btn-warning btn-dialog',
                                //     url: 'order/deposit',
                                //     confirm: '确认退押金？',
                                //     visible: function (row) {
                                //         if(row.status == 3 && row.deposit_status == 1 && row.deposit > 0) return true;
                                //         return false;
                                //     },
                                //     success:function (res){
                                //         // table.bootstrapTable('refresh');
                                //         parent.Toastr.success("退押金成功！");//成功后提示
                                //         $("a.btn-refresh").trigger("click");
                                //         layer.close(index);
                                //         return false;
                                //     },
                                //     error: function (data, ret) {
                                //         return false;
                                //     },
                                // },
                            {
                                name:'detail',
                                text:'订单详情',
                                title:'订单详情',
                                icon:'fa fa-list',
                                classname:'btn btn-xs btn-primary btn-dialog',
                                url: 'order/detail'
                            }
                        ]}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            //批量取消
            $('.btn-cancel').click(function (){
                var order_ids = Table.api.selectedids(table);
                layer.confirm(__('确认取消'+order_ids.length+'条数据'),{
                    icon:3,
                    btn: ['确认', '取消']
                },function (index){
                    var ids_str = order_ids.length > 0 ? order_ids.join(",") : 0;
                    Fast.api.ajax({
                        url:'order/cancel',
                        data:{ids:ids_str}
                    },function (data,ret) {
                        parent.Toastr.success("取消成功！");//成功后提示
                        $("a.btn-refresh").trigger("click");
                        layer.close(index);
                        return false;
                    },function (data,ret) {
                        parent.Toastr.error(ret.msg);
                        layer.close(index);
                        return false;
                    });
                },function (index){
                    layer.close(index);
                });
            });

            //批量退押金
            $('.btn-deposit').click(function (){
                var order_ids = Table.api.selectedids(table);
                layer.confirm(__('确认退'+order_ids.length+'条数据的押金'),{
                    icon:3,
                    btn: ['确认', '取消']
                },function (index){
                    var ids_str = order_ids.length > 0 ? order_ids.join(",") : 0;
                    Fast.api.ajax({
                        url:'order/deposit',
                        data:{ids:ids_str}
                    },function (data,ret) {
                        parent.Toastr.success("退押金成功！");//成功后提示
                        $("a.btn-refresh").trigger("click");
                        layer.close(index);
                        return false;
                    },function (data,ret) {
                        parent.Toastr.error(ret.msg);
                        layer.close(index);
                        return false;
                    });
                },function (index){
                    layer.close(index);
                });
            });

            //取车到时间改为用车中
            $('.btn-use').click(function (){
                layer.confirm(__('取车时间到期订单，更改为用车中'),{
                    icon:3,
                    btn: ['确认', '取消']
                },function (index){
                    Fast.api.ajax({
                        url:'order/use',
                    },function (data,ret) {
                        parent.Toastr.success("更改成功！");//成功后提示
                        $("a.btn-refresh").trigger("click");
                        layer.close(index);
                        return false;
                    },function (data,ret) {
                        parent.Toastr.error(ret.msg);
                        layer.close(index);
                        return false;
                    });
                },function (index){
                    layer.close(index);
                });
            });
        },

        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        pick: function () {
            Controller.api.bindevent();
        },
        return: function () {
            Controller.api.bindevent();
        },
        change: function () {
            Controller.api.bindevent();
        },
        continue: function () {
            Controller.api.bindevent();
        },
        face: function () {
            Controller.api.bindevent();
        },
        examine: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});